import Vue from 'vue'
import VueRouter, { RouteConfig } from 'vue-router'
import Home from '../components/home/home.vue'
import login from '../components/login.vue'
import welcome from '../components/home/home_container/welcome.vue'
import users from '../components/home/home_container/user/users.vue'
import Rights from '../components/home/home_container/power/Rights.vue'
import Roles from '../components/home/home_container/power/Roles.vue'
Vue.use(VueRouter)

const routes: Array<RouteConfig> = [
  {path:'/',
  redirect:'/login'
  },
  {path:'/login',
   component:login
  },
  {path:'/home',
  component:Home,
  redirect:'/welcome',
  children:[{
    path:'/welcome',component:welcome
  },{
    path:'/users',component:users
  },{
    path:'/rights',component:Rights
  },{
    path:'/roles',component:Roles
  }
  ]
 }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
// 挂载路由导航守卫
// router.beforeEach((to,from,next) => {
//   // to代表将要访问的路径
//   // from代表从哪个路径跳转而来
//   // next是一个函数 表示放行
//   // next（）放行 next（'/login'）代表强制跳转
//   if(to.path === './login') return next();
//   // 获取token
//   const tokenItem = window.sessionStorage.getItem('token');
//   if(!tokenItem) return next('./login');
//   next();
// })
router.beforeEach((to,from,next) => {
  if(to.path === '/login') return next()
  const TokenStr = window.sessionStorage.getItem('token');
  if(!TokenStr) return next('/login')
  next();
})

export default router
